//
//  MiddleView.swift
//  QQZone
//
//  Created by xmg on 16/9/1.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

private let ratio : CGFloat = 0.35

protocol MiddleViewDelegate : class{

    func middleView(middleV : MiddleView ,index : Int)
}

class MiddleView: UIView {

    /** 临时存储btn */
    var selectItem : TabbarButton?
    /** 设置代理 */
    var delegate : MiddleViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpItem("tab_bar_feed_icon", title: "全部动态")
            
        setUpItem("tab_bar_passive_feed_icon", title: "与我相关")

        setUpItem("tab_bar_pic_wall_icon", title: "照片墙")

        setUpItem("tab_bar_e_album_icon", title: "电子相框")

        setUpItem("tab_bar_friend_icon", title: "好友")

        setUpItem("tab_bar_e_more_icon", title: "更多")

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension MiddleView{

    /** 设置图片的文字 */
    func setUpItem(iconName :String ,title : String){
        /** 创建按钮 */
        let item = TabbarButton()
        /** 设置按钮属性 */
        item.setImage(UIImage(named: iconName), forState: .Normal)
        item.setTitle(title, forState: .Normal)
        
        item.setBackgroundImage(UIImage(named: "tabbar_separate_selected_bg"), forState: .Selected)
        item.imageView?.contentMode = .Center
        item.addTarget(self, action: "itemClick:", forControlEvents: .TouchDown)
        /** 绑定tag */
        item.tag = subviews.count
        addSubview(item)
    }
    
    @objc private func itemClick(item : TabbarButton){
        selectItem?.selected = false;
        selectItem = item
        selectItem?.selected = true
        delegate?.middleView(self, index: item.tag)
    }
    
    /** 监听屏幕方向改变,设置frame */
    func setUpScreenDirection(isBool :Bool){
    
        /** 获取子控制的个数 */
        let count = subviews.count
        /** 设置frame */
        let W = superview?.frame.width
        let H = CGFloat(count) * kDockItemH
        let X : CGFloat = 0
        let Y : CGFloat = 0
        frame = CGRect(x: X, y: Y, width: W!, height: H)
        
        /** 设置只控制器的frame */
        for i in 0..<count{
        
            let item = subviews[i] as! TabbarButton
            let itemW : CGFloat = W!
            let itemH : CGFloat = kDockItemH
            let itemX : CGFloat = 0
            let itemY : CGFloat = CGFloat(i) * itemH
            item.isBool = isBool
            item.frame = CGRect(x: itemX, y: itemY, width: itemW, height: itemH)
        }
        
    }
}
/** 设置Button按钮 */
class  TabbarButton : UIButton {
    
    /** 重写点击高亮关闭 */
    override var highlighted : Bool{
        didSet{
        super.highlighted = false
        }
    }
    var isBool : Bool = false
    override func layoutSubviews() {
        super.layoutSubviews()
        if isBool{
            /** 设置按钮图片位置 */
            imageView?.frame = CGRect(x: 0, y: 0, width: ratio * frame.width, height: frame.height)
            /** 设置文字位置 */
            titleLabel?.frame = CGRect(x: ratio
                * frame.width, y: 0, width: (1 - ratio) * frame.width, height: frame.height)
        }else{
            /** 设置图片位置 */
            imageView?.frame = bounds
            /** 隐藏文字 */
            titleLabel?.frame = CGRectZero
        }
    }
}